using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[RequireComponent(typeof(MeshCollider))]
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class InitScene : MonoBehaviour {
	
	private List<GameObject> m_BookSheets;                                    
	private int leftHeight, rightHeight;                        
	private bool pageAnimInProgress;                            
	private int pageAnimIndex;
	
	public PagePropertie currentPage;                       
	
	private GameObject hero;

    private CutBlock cutBlock;
    private Platform leftPlatform;
    private Platform topRightPlatform;
    private Platform topRightPlatform2;
    private Platform bottomRightPlatform;

    private bool isCutEnabled;

    void Start()
    {
		//relative heigth
		leftHeight = 1;
		rightHeight = 2;
		
		pageAnimInProgress = false;
        isCutEnabled = false;
		
        Debug.Log("Empty Start");
        Debug.Log("Creation of all the squares");
		m_BookSheets = new List<GameObject>();                              
		
        // Initialisation de la page de gauche
		GameObject currentSheet = new GameObject();                         
        currentSheet.AddComponent("Sheet");
        ((Sheet)currentSheet.GetComponent("Sheet")).Build(3, 4, false, 1, Color.white);   
        currentSheet.transform.Translate(new Vector3(0f, 0f, 1f));         
        m_BookSheets.Add(currentSheet);

        // Initialisation de la page de droite
        currentSheet = new GameObject();                                     
        currentSheet.AddComponent("Sheet");
        ((Sheet)currentSheet.GetComponent("Sheet")).Build(3, 4, true, 1, Color.white);
		currentSheet.transform.Translate(new Vector3(3.001f,0f,1f));
		m_BookSheets.Add(currentSheet);

        // Initialisation de la page du milieu
        currentSheet = new GameObject();
        currentSheet.AddComponent("Sheet");
        ((Sheet)currentSheet.GetComponent("Sheet")).Build(3, 4, false, 2, Color.grey);
		currentSheet.transform.Translate(new Vector3(3.001f,0f,1-1/1000f));
		m_BookSheets.Add(currentSheet);
		
		currentPage = new PagePropertie(1f,0f,6f,true,false,0,2);
		currentPage.rightPage = new PagePropertie(1f,0f,6f,false,true,2,1);
		currentPage.rightPage.leftPage = currentPage;
		currentPage.rightPage.rightPage = null;
		currentPage.leftPage = null;
		
		// Initialisation du hero
		Debug.Log("Hero creation");
		hero = new GameObject();
		hero.AddComponent("HeroManager");
		((HeroManager)hero.GetComponent("HeroManager")).Build();
		
		hero.transform.Translate(new Vector3(0,0,1f - 1.5f/1000f));
		((HeroManager)hero.GetComponent("HeroManager")).currentPage = currentPage;

        // Initialisation d'un bloc de decoupage
        Debug.Log("CutBlock creation");
        GameObject cutBlockGO = new GameObject();
        cutBlock = cutBlockGO.AddComponent<CutBlock>();
        cutBlock.Build();

        Vector3 topRightPagePosition = m_BookSheets[2].transform.position;
        cutBlock.transform.Translate(topRightPagePosition.x, topRightPagePosition.y, topRightPagePosition.z - 1.5f / 1000f);
        cutBlock.transform.parent = m_BookSheets[2].transform;
        cutBlock.currentPageProperties = currentPage.rightPage;

        // Initialisation de la plateforme de gauche
        Debug.Log("Left platform creation");
        GameObject platformGO = new GameObject();
        leftPlatform = platformGO.AddComponent<Platform>();
        leftPlatform.Build(true);

        Vector3 leftPagePosition = m_BookSheets[0].transform.position;
        leftPlatform.transform.Translate(leftPagePosition.x, leftPagePosition.y, leftPagePosition.z - 1.5f / 1000f);
        leftPlatform.transform.parent = m_BookSheets[0].transform;
        leftPlatform.currentPageProperties = currentPage;

        // Initialisation de la plateforme de droite (top)
        Debug.Log("Top right platform creation");
        GameObject topRightPlatformGO = new GameObject();
        GameObject topRightPlatformGO2 = new GameObject();
        topRightPlatform = topRightPlatformGO.AddComponent<Platform>();
        topRightPlatform2 = topRightPlatformGO2.AddComponent<Platform>();
        topRightPlatform.Build(true);
        topRightPlatform2.Build(false);

        topRightPlatform.transform.Translate(topRightPagePosition.x, topRightPagePosition.y, topRightPagePosition.z - 1.5f / 1000f);
        topRightPlatform.transform.parent = m_BookSheets[2].transform;
        topRightPlatform.currentPageProperties = currentPage.rightPage;

        topRightPlatform2.transform.Translate(topRightPagePosition.x, topRightPagePosition.y, topRightPagePosition.z + 1.5f / 1000f);
        topRightPlatform2.transform.parent = m_BookSheets[2].transform;
        topRightPlatform2.currentPageProperties = currentPage.rightPage;

        // Initialisation de la plateforme de droite (bottom)
        Debug.Log("Bottom right platform creation");
        GameObject bottomRightPlatformGO = new GameObject();
        bottomRightPlatform = bottomRightPlatformGO.AddComponent<Platform>();
        bottomRightPlatform.Build(true);

        Vector3 bottomRightPagePosition = m_BookSheets[2].transform.position;
        bottomRightPlatform.transform.Translate(bottomRightPagePosition.x, bottomRightPagePosition.y, bottomRightPagePosition.z - 1.5f / 1000f);
        bottomRightPlatform.transform.parent = m_BookSheets[1].transform;
        bottomRightPlatform.currentPageProperties = currentPage.rightPage;

        // Initialisation des numeros de page
        GameObject pageOneIndexGO = new GameObject();
        Square pageOneIndex = pageOneIndexGO.AddComponent<Square>();
        pageOneIndex.Build(new Vector3(0, 0, 0),
                           new Vector3(0, 1, 0),
                           new Vector3(1, 1, 0),
                           new Vector3(1, 0, 0), new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0), "PageOneIndex", "PageOneIndex");
        pageOneIndex.transform.Translate(new Vector3(leftPagePosition.x + 1, leftPagePosition.y - 0.2f, leftPagePosition.z - 0.0001f));
        pageOneIndex.transform.parent = m_BookSheets[0].transform;
        pageOneIndex.renderer.material = (Material)Resources.Load("PageOneIndexMat");

        GameObject pageTwoIndexGO = new GameObject();
        Square pageTwoIndex = pageTwoIndexGO.AddComponent<Square>();
        pageTwoIndex.Build(new Vector3(0, 0, 0),
                           new Vector3(0, 1, 0),
                           new Vector3(1, 1, 0),
                           new Vector3(1, 0, 0), new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0), "PageTwoIndex", "PageTwoIndex");
        pageTwoIndex.transform.Translate(new Vector3(topRightPagePosition.x + 1, topRightPagePosition.y - 0.2f, topRightPagePosition.z - 1.5f / 1000f));
        pageTwoIndex.transform.parent = m_BookSheets[2].transform;
        pageTwoIndex.renderer.material = (Material)Resources.Load("PageTwoIndexMat");

        GameObject pageThreeIndexGO = new GameObject();
        Square pageThreeIndex = pageThreeIndexGO.AddComponent<Square>();
        pageThreeIndex.Build(new Vector3(0, 0, 0),
                           new Vector3(1, 0, 0),
                           new Vector3(1, 1, 0),
                           new Vector3(0, 1, 0),

                           new Vector2(1, 0),
                           new Vector2(0, 0),
                           new Vector2(0, 1),
                           new Vector2(1, 1),
                           
                           "PageThreeIndex", "PageThreeIndex");

        pageThreeIndex.transform.Translate(topRightPagePosition.x + 1, topRightPagePosition.y - 0.2f, topRightPagePosition.z + 1.5f / 1000f);
        pageThreeIndex.transform.parent = m_BookSheets[2].transform;
        pageThreeIndex.renderer.material = (Material)Resources.Load("PageThreeIndexMat");

        GameObject pageFourIndexGO = new GameObject();
        Square pageFourIndex = pageFourIndexGO.AddComponent<Square>();
        pageFourIndex.Build(new Vector3(0, 0, 0),
                           new Vector3(0, 1, 0),
                           new Vector3(1, 1, 0),
                           new Vector3(1, 0, 0), new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0), "PageFourIndex", "PageFourIndex");

        pageFourIndex.transform.Translate(bottomRightPagePosition.x + 1, bottomRightPagePosition.y - 0.2f, bottomRightPagePosition.z - 0.00005f);
        pageFourIndex.transform.parent = m_BookSheets[1].transform;
        pageFourIndex.renderer.material = (Material)Resources.Load("PageFourIndexMat");
    }
	
	private void bindHero()
	{
		if(hero.transform.position.x < 3f)
		{
			hero.transform.parent = m_BookSheets[currentPage.leftPageIndex].transform;
		}
		else
		{
			hero.transform.parent = m_BookSheets[currentPage.rightPageIndex].transform;	
		}
	}
	
	// Update is called once per frame
	void Update () {
		if(pageAnimInProgress)
		{
			if(! ((Sheet)m_BookSheets[pageAnimIndex].GetComponent("Sheet")).isTurning() )
			{
				pageAnimInProgress = false;
			}
		}
		
		if(!pageAnimInProgress /*&& canTurnPage()*/)
		{
			//bindHero();
			if(Input.GetKeyDown(KeyCode.LeftArrow))
			{
				if(currentPage.canTurnLeft)
				{
					--rightHeight;
					++leftHeight;
					((Sheet)m_BookSheets[currentPage.rightPageIndex].GetComponent("Sheet")).TurnLeft(leftHeight);
					
					pageAnimInProgress = true;
					pageAnimIndex = currentPage.rightPageIndex;
					currentPage = currentPage.rightPage;
					
					/*leftPage = rightPage;
					--rightPage;*/
					return;
				}
			} 
			
			if(Input.GetKeyDown(KeyCode.RightArrow))
			{
				if(currentPage.canTurnRight)
				{
					++rightHeight;
					--leftHeight;
					((Sheet)m_BookSheets[currentPage.leftPageIndex].GetComponent("Sheet")).TurnRight(rightHeight);
					
					pageAnimInProgress = true;
					pageAnimIndex = currentPage.leftPageIndex;
					
					currentPage = currentPage.leftPage;
					
					/*rightPage = leftPage;
					++leftPage;*/
				}
			}

            if (Input.GetKeyDown(KeyCode.Return) && cutBlock != null)
            {
                if (!isCutEnabled)
                {
                    if (cutBlock.cut(m_BookSheets[currentPage.rightPageIndex].GetComponent<Sheet>()) > 0)
                        isCutEnabled = true;
                }
                else
                {
                    cutBlock.uncut();
                    isCutEnabled = false;
                }
            }

            if (cutBlock != null && cutBlock.intersect(((HeroManager)hero.GetComponent("HeroManager"))))
            {

            }
			
		}
		
	}
}
